<h:html xmlns="http://www.w3.org/2002/xforms" xmlns:ev="http://www.w3.org/2001/xml-events" xmlns:h="http://www.w3.org/1999/xhtml" xmlns:jr="http://openrosa.org/javarosa" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <h:head>
    <h:title><%= @form.full_name %></h:title>
    <model>
      <instance>
        <data id="<%= @form.id %>">
          <% @form.visible_questionings.each do |q| %>
            <%= tag(q.question.odk_code) %>
          <% end %>
        </data>
      </instance>
      <itext>
        <% configatron.languages.each do |lang| %>
          <translation lang="<%= LanguageList::LANGS[lang] %>">
            <% @form.visible_questionings.each do |q| %>
              <text id="<%= q.question.odk_code %>:label">
                <value><%= q.question.name(lang) %></value>
              </text>
              <text id="<%= q.question.odk_code %>:hint">
                <value><%= q.question.hint(lang) %></value>
              </text>
            <% end %>
            <% @form.option_sets.collect{|set| set.options}.flatten.uniq{|o| o.id}.each do |opt| %>
              <text id="option<%= opt.id %>">
                <value><%= opt.name(lang) %></value>
              </text>
            <% end %>
          </translation>
        <% end %>
      </itext>
      <% @form.visible_questionings.each do |q| %>
        <%= tag("bind", {
            'nodeset' => "/data/#{q.question.odk_code}",
            'type' => q.question.type.odk_name,
            'required' => q.required? ? "true()" : nil,
            'relevant' => q.has_condition? ? q.condition.to_odk : nil,
            'constraint' => q.odk_constraint,
            'jr:constraintMsg' => q.question.min_max_error_msg,
           }.reject{|k,v| v.nil?}).gsub(/"required"/, '"true()"').html_safe %>
      <% end %>
    </model>
  </h:head>
  <h:body>
    <% @form.visible_questionings.each do |q| %>
      <% next if q.question.type.odk_tag.nil? %>
      <%= content_tag(q.question.type.odk_tag, :ref => "/data/#{q.question.odk_code}") do %>
        <label ref="jr:itext('<%= q.question.odk_code %>:label')"/>
        <hint ref="jr:itext('<%= q.question.odk_code %>:hint')"/>
        <% if set = q.question.option_set %>
          <% set.sorted_options.each do |opt| %>
            <item>
              <label ref="jr:itext('option<%= opt.id %>')"/>
              <value><%= opt.id %></value>
            </item>
          <% end %>
        <% end %>
      <% end %>
    <% end %>
  </h:body>
</h:html>